Skip to content

Commit 7b4164a

Browse files
authored
LaTeX: better choice for \tymax parameter (#13711)
1 parent 52c4d72 commit 7b4164a

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

doc/usage/restructuredtext/directives.rst

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,8 +1499,31 @@ Check the :confval:`latex_table_style`.
14991499
using ``tabulary`` is that it tries to compute automatically (internally to
15001500
LaTeX) suitable column widths.
15011501

1502-
:rst:dir:`tabularcolumns` can serve to provide one's own "colspec" choice.
1503-
Here is an advanced example:
1502+
The ``tabulary`` algorithm often works well, but in some cases when a cell
1503+
contains long paragraphs, the column will be given a large width and other
1504+
columns whose cells contain only single words may end up too narrow. The
1505+
:rst:dir:`tabularcolumns` can help solve this via providing to LaTeX a
1506+
custom "alignment preamble" (aka "colspec"). For example ``lJJ`` will be
1507+
suitable for a three-columns table whose first column contains only single
1508+
words and the other two have cells with long paragraphs.
1509+
1510+
.. note::
1511+
1512+
Of course, a fully automated solution would be better, and it is still
1513+
hoped for, but it is an intrinsic aspect of ``tabulary``, and the latter
1514+
is in use by Sphinx ever since ``0.3``... It looks as if solving the
1515+
problem of squeezed columns could require substantial changes to that
1516+
LaTeX package. And no good alternative appears to exist, as of 2025.
1517+
1518+
.. hint::
1519+
1520+
A way to solve the issue for all tables at once, is to inject in the
1521+
LaTeX preamble (see :confval:`latex_elements`) a command such as
1522+
``\setlength{\tymin}{1cm}`` which causes all columns to be at least
1523+
``1cm`` wide (not counting inter-column whitespace). Currently, Sphinx
1524+
configures ``\tymin`` to allow room for three characters at least.
1525+
1526+
Here is a more sophisticated "colspec", for a 4-columns table:
15041527

15051528
.. code-block:: latex
15061529

sphinx/texinputs/sphinxlatextables.sty

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@
267267
%
268268
% configuration of tabulary
269269
\setlength{\tymin}{3\fontcharwd\font`0 }% minimal width of "squeezed" columns
270-
\setlength{\tymax}{2\textwidth}% allow enough room for paragraphs to "compete"
270+
\setlength{\tymax}{3000pt}% allow enough room for paragraphs to "compete"
271271
%
272272
% MEMO: tabulary initially renders cell contents "horizontally" to measure
273273
% them and compare their relative importance. Its goal is to choose the
@@ -284,11 +284,14 @@
284284
% the initial horizontal width for "varwidth". In the first tabulary
285285
% pass, \sphinxcolwidth is configured (by us) to use \tymax.
286286
%
287-
% During testing, it was determined that our former 10000pt setting for
288-
% \tymax could cause "Dimension too large" TeX error if two columns or
289-
% more contained such cells. So we use now 2\textwidth which is more
290-
% than 10 times smaller but proves large enough for the tabulary
291-
% algorithm to provide reasonable results.
287+
% During testing, it was determined that the former 10000pt setting for
288+
% \tymax would cause "Dimension too large" TeX error if two columns or
289+
% more had cells containing admonitions (such contents does not allow
290+
% "varwidth" to reduce the width automatically). So we use now 3000pt
291+
% which allows up to 5 such columns while being large enough for
292+
% tabulary algorithm to give good results for cells containing a few
293+
% dozen words. The tabulary default of 2\textwidth proves to be too
294+
% small for that.
292295
%
293296
% we need access to tabulary's final computed width. \@tempdima is too volatile
294297
% to hope it has kept tabulary's value when \sphinxcolwidth needs it.

0 commit comments

Comments
 (0)